Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\api\config\core.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::api::config::model::Config;
30
use crate::api::core::generator::{Context, Generator, Params};
31
use crate::api::core::loader::Loader;
32
use crate::api::core::Error;
33
use crate::compiler::util::imports::{ImportSolver, ProtocolStore};
34
use bp3d_debug::trace;
35
use serde::Deserialize;
36
use std::borrow::Cow;
37
38
2
pub fn parse<'a, T: Deserialize<'a>>(data: &'a str) -> Result<Config<'a, T>, toml::de::Error> {
39
2
    toml::from_str(data)
40
2
}
41
42
2
pub fn compile<'a, T, I: ImportSolver>(config: &Config<T>, solver: &'a I) -> Result<ProtocolStore<'a, I>, Error> {
43
2
    let mut loader = Loader::default();
44
2
    loader.load_from_folder(config.package.path, config.package.name)
?0
;
45
2
    if let Some(
deps0
) = &config.dependency {
  Branch (45:12): [True: 0, False: 1]
  Branch (45:12): [Folded - Ignored]
  Branch (45:12): [True: 0, False: 1]
  Branch (45:12): [Folded - Ignored]
46
0
        for dep in deps {
47
0
            loader.load_from_file(dep.path, dep.package)?;
48
        }
49
2
    }
50
2
    trace!("Excluded protocols: {:?}", config.package.exclude);
51
2
    if let Some(
excludes1
) = &config.package.exclude {
  Branch (51:12): [True: 1, False: 0]
  Branch (51:12): [Folded - Ignored]
  Branch (51:12): [True: 0, False: 1]
  Branch (51:12): [Folded - Ignored]
52
2
        for 
exclude1
in excludes {
53
1
            loader.exclude(exclude);
54
1
        }
55
1
    }
56
2
    loader.compile(solver)
57
2
}
58
59
2
pub fn generate<'a, G: crate::gen::Generator, T, I: ImportSolver, F: Fn(&T) -> Option<G::Params<'_>>>(
60
2
    generator: &'a Generator<'a, G>,
61
2
    context: &mut Context<'a, I>,
62
2
    config: &Config<T>,
63
2
    generator_params_converter: F,
64
2
    generator_default_params: &G::Params<'_>,
65
2
) -> Result<(), Error> {
66
2
    if let Some(options) = &config.options {
  Branch (66:12): [True: 2, False: 0]
  Branch (66:12): [True: 0, False: 0]
  Branch (66:12): [Folded - Ignored]
67
12
        for (
protocol, params10
) in options {
68
10
            let protocol_path: Cow<str> = if config.package.name.is_empty() {
  Branch (68:46): [True: 10, False: 0]
  Branch (68:46): [True: 0, False: 0]
  Branch (68:46): [Folded - Ignored]
69
10
                Cow::Borrowed(protocol)
70
            } else {
71
0
                Cow::Owned(format!("{}::{}", config.package.name, protocol))
72
            };
73
10
            trace!({ path = &*protocol_path }, "Found options for protocol: {}", protocol);
74
10
            let mut p = Params::default();
75
10
            if let Some(
flag1
) = params.write_messages {
  Branch (75:20): [True: 1, False: 9]
  Branch (75:20): [True: 0, False: 0]
  Branch (75:20): [Folded - Ignored]
76
1
                p.write_messages = flag;
77
9
            }
78
10
            if let Some(
flag1
) = params.read_messages {
  Branch (78:20): [True: 1, False: 9]
  Branch (78:20): [True: 0, False: 0]
  Branch (78:20): [Folded - Ignored]
79
1
                p.read_messages = flag;
80
9
            }
81
10
            if let Some(
flag0
) = params.use_enums {
  Branch (81:20): [True: 0, False: 10]
  Branch (81:20): [True: 0, False: 0]
  Branch (81:20): [Folded - Ignored]
82
0
                p.use_enums = flag;
83
10
            }
84
10
            if let Some(
flag0
) = params.use_structs {
  Branch (84:20): [True: 0, False: 10]
  Branch (84:20): [True: 0, False: 0]
  Branch (84:20): [Folded - Ignored]
85
0
                p.use_structs = flag;
86
10
            }
87
10
            if let Some(
flag0
) = params.use_unions {
  Branch (87:20): [True: 0, False: 10]
  Branch (87:20): [True: 0, False: 0]
  Branch (87:20): [Folded - Ignored]
88
0
                p.use_unions = flag;
89
10
            }
90
10
            if let Some(
flag0
) = params.use_messages {
  Branch (90:20): [True: 0, False: 10]
  Branch (90:20): [True: 0, False: 0]
  Branch (90:20): [Folded - Ignored]
91
0
                p.use_messages = flag;
92
10
            }
93
10
            if let Some(
gp9
) = generator_params_converter(&params.inner) {
  Branch (93:20): [True: 9, False: 1]
  Branch (93:20): [True: 0, False: 0]
  Branch (93:20): [Folded - Ignored]
94
9
                context.generate(generator, protocol_path, &p, &gp)
?0
;
95
            } else {
96
1
                context.generate(generator, protocol_path, &p, generator_default_params)
?0
;
97
            }
98
        }
99
0
    }
100
2
    let params = Params::default();
101
2
    context.generate_all(generator, &params, generator_default_params)
102
2
}